﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;

namespace ChapterX.Common
{
    public class Logger
    {
        private static String LogFileDir = HttpContext.Current.Server.MapPath(@"~/logs/");

        public static void Log(String text)
        {
            try
            {
                String logFilePath = LogFileDir + "log-" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";

                text = String.Format("{0} {1}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), text.Replace("\r\n", "\\r\\n"));

                lock (LogFileDir)
                {
                    using (StreamWriter sw = new StreamWriter(File.Open(logFilePath, FileMode.Append)))
                    {
                        sw.Write(text);
                        sw.Flush();
                        sw.Close();
                    }
                }
            }
            catch
            {
                // Oops!
            }
        }

        public static void Log(Exception ex)
        {
            Log(ex.ToString());
        }

        public static void Log(String format, params object[] args)
        {
            Log(String.Format(format, args));
        }
    }
}


